package gov.mogaha.ntis.cmm.mob.law.handler;

import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawStrUtil;
import gov.mogaha.ntis.cmm.mob.law.vo.CmmMobLawVO;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class CmmMobLawXmlHandler extends DefaultHandler{
	
	ArrayList<CmmMobLawVO> values;
	CmmMobLawVO vo;
	
	String curEl;
	
	public CmmMobLawXmlHandler(){
		this.values = new ArrayList<CmmMobLawVO>();
	}
	
	@Override
	public void endDocument() throws SAXException {
		//Log.i(CmmMobLawStrUtil.INFO, "Parse End");
	}

	@Override
	public void startDocument() throws SAXException {
		//Log.i(CmmMobLawStrUtil.INFO, "Parse Strart");
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		
		String strChar = CmmMobLawStrUtil.characters(ch, start, length).toString();
		strChar = strChar.replaceAll("<strong>", "").replaceAll("</strong>", "");
		
		if (vo != null && strChar != null && !strChar.trim().equals("")) {
			
			if(curEl.equalsIgnoreCase("법령일련번호")){
				vo.lawNo = strChar; 
			}
			else if (curEl.equalsIgnoreCase("법령명한글")) {
				vo.lawNm= strChar;
			}
			else if (curEl.equalsIgnoreCase("공포일자")) {
				vo.lawYmd = strChar;
			}
			else if (curEl.equalsIgnoreCase("법령구분명")) {
				vo.lawTypeNm = strChar;
			}
			else if (curEl.equalsIgnoreCase("법령상세링크")) {
				vo.lawUrl = CmmMobLawStrUtil.Nvl(vo.lawUrl, "") + strChar;
			}
		}
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		curEl = localName;
		
		if(localName.equalsIgnoreCase("law") || localName.equalsIgnoreCase("admrul") || localName.equalsIgnoreCase("ordin")
			 || localName.equalsIgnoreCase("trty") || localName.equalsIgnoreCase("prec")  || localName.equalsIgnoreCase("licbyl")
			 || localName.equalsIgnoreCase("lstrm")){
			
			vo = new CmmMobLawVO();
		}
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(localName.equalsIgnoreCase("law") || localName.equalsIgnoreCase("admrul") || localName.equalsIgnoreCase("ordin")
				 || localName.equalsIgnoreCase("trty") || localName.equalsIgnoreCase("prec")  || localName.equalsIgnoreCase("licbyl")
				 || localName.equalsIgnoreCase("lstrm")){
			values.add(vo);			
		}
	}
	
	public ArrayList<CmmMobLawVO> getList(){
		return this.values;
	}

}